package cn.iocoder.yudao.module.cmt.controller.admin.notify;

import cn.iocoder.yudao.module.cmt.service.notify.CmtNotifyNoticeService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.notify.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.notify.CmtNotifyNoticeDO;

@Tag(name = "管理后台 - 公告")
@RestController
@RequestMapping("/cmt/notify-notice")
@Validated
public class CmtNotifyNoticeController {

    @Resource
    private CmtNotifyNoticeService noticeService;

    @PostMapping("/create")
    @Operation(summary = "创建公告")
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:create')")
    public CommonResult<Long> createNotice(@Valid @RequestBody CmtNotifyNoticeSaveReqVO createReqVO) {
        return success(noticeService.createNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公告")
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:update')")
    public CommonResult<Boolean> updateNotice(@Valid @RequestBody CmtNotifyNoticeSaveReqVO updateReqVO) {
        noticeService.updateNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:delete')")
    public CommonResult<Boolean> deleteNotice(@RequestParam("id") Long id) {
        noticeService.deleteNotice(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公告")
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:delete')")
    public CommonResult<Boolean> deleteNoticeList(@RequestParam("ids") List<Long> ids) {
        noticeService.deleteNoticeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:query')")
    public CommonResult<CmtNotifyNoticeRespVO> getNotice(@RequestParam("id") Long id) {
        CmtNotifyNoticeDO notice = noticeService.getNotice(id);
        return success(BeanUtils.toBean(notice, CmtNotifyNoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公告分页")
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:query')")
    public CommonResult<PageResult<CmtNotifyNoticeRespVO>> getNoticePage(@Valid CmtNotifyNoticePageReqVO pageReqVO) {
        PageResult<CmtNotifyNoticeDO> pageResult = noticeService.getNoticePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CmtNotifyNoticeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公告 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:notify-notice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNoticeExcel(@Valid CmtNotifyNoticePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CmtNotifyNoticeDO> list = noticeService.getNoticePage(pageReqVO).getList();
        ExcelUtils.write(response, "公告.xls", "数据", CmtNotifyNoticeRespVO.class,
                        BeanUtils.toBean(list, CmtNotifyNoticeRespVO.class));
    }

}
